﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace MulticastTV
{
    class ExportToM3uTano
    {
        public void Start(string file, string datasetname)
        {
            bool same_number = false;
            List<string> channel_with_same_number = new List<string>();
            StreamWriter sw = File.CreateText(file);
            sw.WriteLine("#EXTM3U");
            sw.WriteLine("#EXTNAME:"+datasetname);
            sw.WriteLine();

            int prevchan = 0;
            string prevname = "";
            DataView dw = ChannelTable.menu.Tables["Menu"].DefaultView;
            dw.Sort = "Chan";
            foreach (DataRowView dr in dw)
            {
                string ip = dr[0].ToString();
                int ch = (int)dr[1];
                string name = FirstToUpper(dr[2].ToString(),false);
                string audio = dr[4].ToString();
                string group = FirstToUpper(dr[3].ToString(),true);
                string epgid = "";
                string dictout = "";
                if (pluginNames.channel!=null &&  pluginNames.channel.TryGetValue(name, out dictout))
                    epgid = dictout;

                // Note that Tano format uses "length" field for "channel number" !
  
                sw.WriteLine("#EXTINF:" + ch + "," + name);
                sw.WriteLine("#EXTTV:" + group + ";" + audio + ";" + epgid );
                sw.WriteLine("udp://@" + ip);
                sw.WriteLine();

                if (prevchan == ch && ch != 0)
                {
                    same_number = true;
                    channel_with_same_number.Add(prevchan.ToString() + "." + prevname);
                    channel_with_same_number.Add(ch.ToString() + "." + name);
                }
                prevchan = ch;
                prevname = name;

            }
            sw.Close();

            // Just notify user, don't reorder !
            if (same_number)
            {
                string names = "";
                foreach (string n in channel_with_same_number)
                {
                    names = names + " " + n;
                }
                MessageBox.Show("Some channels have the same number! ( " + names + " )");
            }
        }

        private string FirstToUpper(string input, bool commasep)
        {
            if (input.Length > 0)
            {
                if (commasep)
                {
                    string[] values = input.Split(",".ToCharArray());
                    string output="";
                    foreach (string value in values)
                    {
                        string temp = value.Substring(0, 1);
                        string ValToUp = temp.ToUpper() + value.Remove(0, 1) + ",";
                        output = output + ValToUp;
                    }
                    output = output.Trim(",".ToCharArray());
                    return output;
                }
                else
                {
                    string temp = input.Substring(0, 1);
                    return temp.ToUpper() + input.Remove(0, 1);
                }
            }
            else
                return input;
        }
    }
}